using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Collections;
using NovodexWrapper;

namespace marbletrack
{
    public class Ball : PhysicsControlledObject
    {
        private float ballRadius_;
        public Ship ship_ = null;
        private Vector3 startingPosition_ = Game1.StartingPosition + new Vector3(0, Game1.SQUARESIZE * 2f, Game1.SQUARESIZE);
        public Vector3 StartingPosition
        {
            get { return startingPosition_; }
        }

        public Ball()
        {
            //Initialize all the balls variables with default position
            Matrix transform = Matrix.CreateTranslation(startingPosition_);
            SetupVariables(transform);

            model_.FlagTextured = true;
            //model_.LoadTexture(Game1.redwhite);
        }

        protected override void SetupVariables(Matrix transform)
        {
            model_ = Game1.BallModel;
            density_ = 1000;
            ballRadius_ = 67f;
            transform_ = transform;

            //Create ball actor from descriptors
            NxShapeDesc ballShapeDesc = new NxSphereShapeDesc(NxMat34.Identity, NxShapeFlag.USE_DEFAULT, 0, Physics.BallMaterial.getMaterialIndex(), "marble", ballRadius_);
            NxBodyDesc ballBodyDesc = new NxBodyDesc();
            NxActorDesc ballActorDesc = new NxActorDesc(ballShapeDesc, ballBodyDesc, density_, MathConversion.ToNxMat34(transform_));
            actor_ = Physics.PhysicsScene.createActor(ballActorDesc);

            //Add to ball actor group
            actor_.setGroup(Physics.BALL_PHYSICS_GROUP);

            //Set AngularDamping so the ball dosen't keep rolling forever
            actor_.AngularDamping = 1f;

            //Increases balls max angular velocity so it will roll down the track properly
            actor_.setMaxAngularVelocity(150);
        }
    }
}